import { defineStore } from 'pinia'
import Cache from '@/global/Cache'
import * as CacheKey from '@/global/CacheKey'

export default defineStore('dynamicRoutes', {
  state: () => {
    return {
      dynamicRoutes: Cache.get(CacheKey.DYNAMIC_ROUTES, JSON.stringify([]))
    };
  },
  actions: {
    loadState() {
      const currentState = Cache.get(CacheKey.DYNAMIC_ROUTES, JSON.stringify([]))
      this.$patch({ dynamicRoutes: JSON.parse(currentState) });
    },
    /**
     * 保存角色信息到缓存并更新组件状态
     *
     * @param newState 新的角色数组
     */
    saveState(newState: any[]) {
      this.dynamicRoutes = newState;
      Cache.save(CacheKey.DYNAMIC_ROUTES, this.dynamicRoutes);
      // this.$patch({ State: newState });
    },
    getState(): any[] {
      console.log(CacheKey.DYNAMIC_ROUTES, []);
      return Cache.get(CacheKey.DYNAMIC_ROUTES, []);
    },
    resetState() {
      this.$reset();
    },
  }
})